package com.lt.flashcard.view;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.Medal;
import com.lt.flashcard.model.entity.MedalHistory;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.LearningService;
import com.lt.flashcard.model.service.Repository;
import com.lt.flashcard.model.service.UserService;

public class ProfileMedalVm {
	
	private User user;
	private boolean owner = false;
	private List<Medal> lStudy;
	private List<Medal> lActivity;
	private List<Medal> lAchive;
	
	private List<String> gotMedal = new ArrayList<String>(); //list of got medals
	private MedalHistory history; //current history to display on the right
	
	private Medal medal;
	private int totalmedals = 0;
	private int tabmedal = 0;

	@Init
	public void init(@ContextParam(ContextType.PAGE) Page page) {
		String uname = Executions.getCurrent().getParameter("uname");
		String Id = Executions.getCurrent().getParameter("id");
		String medalId = Executions.getCurrent().getParameter("medalid");
		
		AAService asrv = Repository.getInstance().getAAService();
		User cuser = asrv.getCurrentUser();
		
		if(Utility.isEmptyString(Id) && Utility.isEmptyString(uname)) {
			if(!asrv.isLogin()) {
				Utility.loginOrAccessDenied(false);
				return;
			}
			else {
				String url = Utility.generateUrl("medal", cuser.getUsername(), null);
    			Utility.redirect(Utility.removeContextPath(url), false);
    			return;
			}
		}
		else {
			UserService usrv = Repository.getInstance().getUserService();
			user = Utility.isEmptyString(uname) ? usrv.findByID(new Long(Id)) : usrv.findByUsername(uname);
			if(user == null) {
				Utility.displayAccessDenied(false);
				return;
			}
		}
		
		if(user != null && asrv.isAdmin(user)){
			if(cuser == null || (cuser != null && !user.getId().equals(cuser.getId()))){
				Utility.displayAccessDenied(false);
				return;
			}
		}
		
		owner = user != null && cuser != null && user.getId().equals(cuser.getId());
		
		//check privacy to access this page
		if(!asrv.checkProfilePrivacy(cuser, user, User.PRIVACYGROUP_MEDAL)) {
			Utility.displayAccessDenied(false);
			return;
		}
		
	
		//load medal list
		LearningService lsrv = Repository.getInstance().getLearningService();
		lStudy = lsrv.getMedalsByCategory(Medal.CATEGORY_STUDY);
		lActivity = lsrv.getMedalsByCategory(Medal.CATEGORY_ACTIVITY);
		lAchive = lsrv.getMedalsByCategory(Medal.CATEGORY_ACHIEVEMENT);
		totalmedals = (lStudy == null ? 0 : lStudy.size()) + (lAchive == null ? 0 : lAchive.size()) + (lActivity == null ? 0 : lActivity.size());
		
		List<Medal> hi = lsrv.getMedalsOfUser(getUser().getId());
		for(Medal m : hi) {
			gotMedal.add(m.getId().toString());
		}

		if(!Utility.isEmptyString(medalId)){
			viewmedal(medalId);
			setTabmedal(medal.getCategory());
			
			Utility.addOgTag(page,
 String.format("Huy chương %s", medal.getName()),
					String.format("%s", medal.getDescription()),
					Utility.loadImageFromGridFS(medal.getLargeThumbnail(), ""));
		}
		else{
			Utility.addOgTag(page,
 String.format("Huy chương của %s", user.getUsername()),
                    String.format("Xem những huy chương mà %s đã đạt được", user.getUsername()),
					Utility.loadImageFromGridFS(user.getAvatar(), Utility.DEF_AVATAR));
		}
	}
	
	@NotifyChange({"medal", "history"})
	@Command
	public void viewmedal(@BindingParam("id") String id) {
        if (StringUtils.isNotBlank(id)) {
			LearningService lsrv = Repository.getInstance().getLearningService();
			medal = lsrv.getMedal(id);
            history = isGot(id) ? lsrv.getMedalhistory(getUser().getId(), medal) : null;
			
			int cat = medal.getCategory();
			if(cat == Medal.CATEGORY_ACHIEVEMENT)
				BindUtils.postNotifyChange(null, null, this, "lAchive");
			else if(cat == Medal.CATEGORY_ACTIVITY)
				BindUtils.postNotifyChange(null, null, this, "lActivity");
			else
				BindUtils.postNotifyChange(null, null, this, "lStudy");
		}
	}
	
	public boolean isOwner() {
		return this.owner;
	}
	
	/**
	 * Check to see if a user got a medal
	 * @param id
	 * @return
	 */
	public boolean isGot(String id) {
		return gotMedal.contains(id);
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Medal> getlStudy() {
		return lStudy;
	}

	public void setlStudy(List<Medal> lStudy) {
		this.lStudy = lStudy;
	}

	public List<Medal> getlActivity() {
		return lActivity;
	}

	public void setlActivity(List<Medal> lActivity) {
		this.lActivity = lActivity;
	}

	public List<Medal> getlAchive() {
		return lAchive;
	}

	public void setlAchive(List<Medal> lAchive) {
		this.lAchive = lAchive;
	}

	public Medal getMedal() {
		return medal;
	}

	public void setMedal(Medal medal) {
		this.medal = medal;
	}

	public int getTotalmedals() {
		return totalmedals;
	}

	public void setTotalmedals(int totalmedals) {
		this.totalmedals = totalmedals;
	}

	public List<String> getGotMedal() {
		return gotMedal;
	}

	public void setGotMedal(List<String> gotMedal) {
		this.gotMedal = gotMedal;
	}

	public MedalHistory getHistory() {
		return history;
	}

	public void setHistory(MedalHistory history) {
		this.history = history;
	}

	public int getTabmedal() {
		return tabmedal;
	}

	public void setTabmedal(int tabmedal) {
		this.tabmedal = tabmedal;
	}
}